package com.zzw.utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {
	/**
	 * 返回文件
	 * @author PRIDE
	 * @param img
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @param toimg
	 * @throws IOException
	 */
	public static void reconsitutionImage(File img,int x1,int y1,int x2,int y2,File toimg) throws IOException{
		try {
			FileInputStream fis = new FileInputStream(img);
			BufferedImage image = ImageIO.read(fis); 
			BufferedImage imgs  = new BufferedImage(x2-x1,y2-y1,image.getType());
			Graphics2D gr = imgs.createGraphics();
			gr.drawImage(image, 0, 0, x2-x1, y2-y1, x1, y1, x2,y2, null);
			gr.dispose();
			ImageIO.write(imgs, "jpg", toimg);
		} catch (Exception e) {
			throw  new IOException(e.getMessage());
		}
	}
	/**
	 * 图片画坐标
	 * @author PRIDE
	 * @param img
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @param out
	 * @throws IOException
	 */
	public static void reconsitutionImage(File img,int x1,int y1,int x2,int y2,OutputStream out) throws IOException{
		try {
			FileInputStream fis = new FileInputStream(img);
			BufferedImage image = ImageIO.read(fis); 
			BufferedImage imgs  = new BufferedImage(image.getWidth(), image.getHeight(),image.getType());
			Graphics2D gr = imgs.createGraphics();
			gr.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0,0, image.getWidth(), image.getHeight(), null);
			gr.setColor(Color.BLACK);
			Stroke st = new BasicStroke(3, BasicStroke.JOIN_BEVEL,BasicStroke.JOIN_BEVEL, 0, new float[] { 16, 4 }, 0);
			gr.setStroke(st);
			gr.drawRect(x1, y1, x2-x1, y2-y1);
			gr.dispose();
			ImageIO.write(imgs, "jpg", out);
		} catch (Exception e) {
			throw  new IOException(e.getMessage());
		}
	}
	
	/**
	 * 返回截图后图片
	 * @author PRIDE
	 * @param img
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @param toimg
	 * @throws IOException
	 */
	public static void reconsitutionNewImage(File img,int x1,int y1,int x2,int y2,OutputStream out) throws IOException{
		try {
			FileInputStream fis = new FileInputStream(img);
			BufferedImage image = ImageIO.read(fis); 
			BufferedImage imgs  = new BufferedImage(x2-x1,y2-y1,image.getType());
			Graphics2D gr = imgs.createGraphics();
			gr.drawImage(image, 0, 0, x2-x1, y2-y1, x1, y1, x2,y2, null);
			gr.dispose();
			ImageIO.write(imgs, "jpg", out);
		} catch (Exception e) {
			throw  new IOException(e.getMessage());
		}finally{
			if(out!=null){
				out.flush();
				out.close();
			}
		}
	}
	
	/**
	 * 返回截图后图片
	 * @author PRIDE
	 * @param img
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @param toimg
	 * @throws IOException
	 */
	public static void reconsitutionNewImage(InputStream img,int x1,int y1,int x2,int y2,OutputStream out) throws IOException{
		try {
			BufferedImage image = ImageIO.read(img); 
			BufferedImage imgs  = new BufferedImage(x2-x1,y2-y1,image.getType());
			Graphics2D gr = imgs.createGraphics();
			gr.drawImage(image, 0, 0, x2-x1, y2-y1, x1, y1, x2,y2, null);
			gr.dispose();
			ImageIO.write(imgs, "jpg", out);
		} catch (Exception e) {
			throw  new IOException(e.getMessage());
		}finally{
			if(img!=null){
				img.close();
			}
		}
	}
	
	/**
	 * 返回原始图像
	 * @param img
	 * @param out
	 * @throws IOException
	 */
	public static void originalImg(File img,OutputStream out) throws IOException{
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(img);
			BufferedImage image = ImageIO.read(fis); 
			BufferedImage imgs  = new BufferedImage(image.getWidth(), image.getHeight(),BufferedImage.TYPE_INT_RGB);
			Graphics2D gr = imgs.createGraphics();
			gr.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0,0, image.getWidth(), image.getHeight(), null);
			gr.dispose();
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam jep=JPEGCodec.getDefaultJPEGEncodeParam(imgs); 
            jep.setQuality(0.3f, true); 
            encoder.encode(imgs, jep); 
		} catch (Exception e) {
			throw  new IOException(e.getMessage());
		}finally{
			if(out!=null){
				out.flush();
				out.close();
			}
			if(fis!=null){
				fis.close();
			}
		}
	}
	/**
	 * 返回原始图像
	 * @param img
	 * @param out
	 * @throws IOException
	 */
	public static void originalImg(InputStream img,OutputStream out) throws IOException{
		try {
			BufferedImage image = ImageIO.read(img); 
			BufferedImage imgs  = new BufferedImage(image.getWidth(), image.getHeight(),image.getType());
			Graphics2D gr = imgs.createGraphics();
			gr.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0,0, image.getWidth(), image.getHeight(), null);
			gr.dispose();
			ImageIO.write(imgs, "jpg", out);
		} catch (Exception e) {
			throw  new IOException(e.getMessage());
		}
	}
}
